package com.ncu.cloundeoffice.service.impl;

import com.ncu.cloundeoffice.mapper.TMenuMapper;
import com.ncu.cloundeoffice.mapper.TMenuRoleMapper;
import com.ncu.cloundeoffice.pojo.ResponseResult;
import com.ncu.cloundeoffice.service.TMenuRoleService;
import com.ncu.cloundeoffice.service.TMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class TMenuRoleServiceImpl implements TMenuRoleService{

    @Autowired
    private TMenuRoleMapper tMenuRoleMapper;

    /**
     * 通过角色id查询菜单id
     * @param rid
     * @return
     */
    @Override
    public List<Integer> getMidByRid(int rid) {
        return tMenuRoleMapper.getMidByRid(rid);
    }

    /**
     * 更新角色菜单
     * @param rid
     * @param mids
     * @return
     */
    @Override
    @Transactional
    public ResponseResult updateMenuRoleByRidAndMid(int rid, int[] mids) {
        tMenuRoleMapper.deleteAllByRid(rid);
        if(mids == null || mids.length == 0){
            return ResponseResult.success("更新成功");
        }
        int count = tMenuRoleMapper.insertMenuRole(rid,mids);
        if(count == mids.length){
            return ResponseResult.success("更新成功");
        }
        return ResponseResult.error("更新失败");
    }
}
